//
//  UIbutton-Extension.swift
//  Whiteboard
//
//  Created by point on 2018/1/4.
//  Copyright © 2018年 point. All rights reserved.
//

import UIKit

extension UIButton {
    
    static func createButton(imageStr:String , imageColor:UIColor , selected:String , imageselectedColor:UIColor , size:CGSize) -> UIButton {
        let button = UIButton()
        let image = UIImage.icon(from: imageStr, color: imageColor, imageSize: size)
        let imageLight = UIImage.icon(from: imageStr, color: imageselectedColor, imageSize: size)
        let selected = UIImage.icon(from: selected, color: imageselectedColor, imageSize: size)
        button.setImage(image, for: .normal)
        button.setImage(imageLight, for: .highlighted)
        button.setImage(selected, for: .selected)
        return button
    }
    
    func centerTextAndImage(spacing: CGFloat) {
        let insetAmount = spacing / 2
        let writingDirection = UIApplication.shared.userInterfaceLayoutDirection
        let factor: CGFloat = writingDirection == .leftToRight ? 1 : -1
        
        self.imageEdgeInsets = UIEdgeInsets(top: 0, left: -insetAmount*factor, bottom: 0, right: insetAmount*factor)
        self.titleEdgeInsets = UIEdgeInsets(top: 0, left: insetAmount*factor, bottom: 0, right: -insetAmount*factor)
        self.contentEdgeInsets = UIEdgeInsets(top: 0, left: insetAmount, bottom: 0, right: insetAmount)
    }
}
